import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
  Tree,
  TreeChildren,
  TreeParent,
  UpdateDateColumn,
} from 'typeorm';
import { Role } from '../../role/entities/role.entity';
import { User } from '../../user/entities/user.entity';

@Entity('sys_role_menu')
@Tree('closure-table')
export class Menu {
  @PrimaryGeneratedColumn('uuid')
  id: string;
  @Column({ type: 'varchar', comment: '菜单别名', unique: true })
  name: string;
  @Column({ type: 'varchar', comment: '菜单图标', default: null })
  icon: string;
  @Column({
    type: 'int',
    comment: '菜单图标 0-false 1-true',
    name: 'icon_svg',
    default: 1,
  })
  iconSvg: number;
  @Column({ type: 'varchar', comment: '菜单名' })
  title: string;
  @Column({
    type: 'varchar',
    comment: '菜单类型 A-目录 B-菜单 C-按钮',
    default: null,
  })
  type: string;
  @Column({ type: 'varchar', comment: '菜单组件路径', default: null })
  component: string;
  @Column({
    type: 'varchar',
    comment: '菜单路由路径',
    name: 'route_path',
    default: null,
  })
  routePath: string;
  @Column({ type: 'varchar', comment: '菜单权限标识', default: null })
  permissions: string;
  @Column({ type: 'int', comment: '菜单tab中固定 0-false 1-true', default: 0 })
  fixed: number;
  @Column({ type: 'int', comment: '菜单是否开启 0-false 1-true', default: 1 })
  keepalive: number;
  @Column({ type: 'int', comment: '菜单排序规则', default: 0 })
  sort: number;
  @Column({
    type: 'int',
    comment: '菜单是否隐藏 0-false 1-true',
    default: 0,
    name: 'is_hidden',
  })
  isHidden: number;
  @Column({
    type: 'int',
    comment: '删除标记 0-未删除 1-删除',
    name: 'is_delete',
    default: 0,
  })
  isDelete: number;
  @TreeParent()
  parent: Menu;
  @TreeChildren()
  children: Menu[];
  @Column({ type: 'varchar', comment: '路由重定向', default: null })
  redirect: string;
  @CreateDateColumn({
    type: 'timestamp',
    name: 'create_date',
    comment: '创建时间',
  })
  createDate: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    name: 'update_date',
    comment: '更新时间',
  })
  updateDate: Date;
  @ManyToMany((type) => Role, (role) => role.menu)
  role: Role;
  @ManyToOne((type) => User, (user) => user.menu, { eager: true })
  user: User;
  @ManyToOne((type) => User, (user) => user.menu, { eager: true })
  updateUser: User;
}
